﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Coding4Fun.Kinect.Wpf;
using System.Threading;
using System.Windows.Threading;
using Microsoft.Kinect;
using KinectedMusic.Kinect;
using System.Windows;
using System.Windows.Controls;
using KinectedMusic.Audio;

namespace KinectedMusic.Visual
{
    class KinectController
    {
        private MainWindow mainWindow;
        private Controller controller;
        private Thread kinectThread;

        private List<IKinectInstrument> kinectInstruments = new List<IKinectInstrument>();
        private int currentKinectInstrument = 0;
        public KinectController(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;

            kinectThread = new Thread(() =>
            {
                controller = new Controller();
                controller.ImageSource += new Action<ColorImageFrame>(ControllerImageSource);
                controller.Person += new Action<ColourPerson>(ControllerPerson);
                System.Windows.Threading.Dispatcher.Run();
            });
            kinectThread.Start();


            AudioControl audioControl = new AudioControl();
            IKinectInstrument kinectInstrument = new TestInstrument();
            if (kinectInstrument.Initialise(audioControl))
            {
                kinectInstruments.Add(kinectInstrument);
            }
            mainWindow.ShowInstrument((UserControl)kinectInstrument);

            audioControl.Start();
        }

        void ControllerPerson(ColourPerson colourPerson)
        {
            kinectInstruments[currentKinectInstrument].SendData(colourPerson);
            //mainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, new Action<FrameworkElement, ColourHand>(MoveEllipseLeft), mainWindow.LeftHand, colourPerson.Points[JointType.HandLeft]);
            //mainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, new Action<FrameworkElement, ColourHand>(MoveEllipseRight), mainWindow.RightHand, colourPerson.Points[JointType.HandRight]);
        }

        //TODO: fix hackk!!
        //private void MoveEllipseLeft(FrameworkElement element, ColourHand point)
        //{
        //    Canvas.SetLeft(element, point.X - element.Width / 2.0);
        //    Canvas.SetTop(element, point.Y - element.Height / 2.0);
        //    element.Opacity = point.DistanceFromBody;


        //    if (point.DistanceFromBody == 1.0f && lastDistanceFromBodyLeft != 1.0f)
        //    {
        //        instrument.AddNote((byte)(point.X/10), .1, 127);
        //    }
        //    lastDistanceFromBodyLeft = point.DistanceFromBody;
        //}

        //private void MoveEllipseRight(FrameworkElement element, ColourHand point)
        //{
        //    Canvas.SetLeft(element, point.X - element.Width / 2.0);
        //    Canvas.SetTop(element, point.Y - element.Height / 2.0);
        //    element.Opacity = point.DistanceFromBody;


        //    if (point.DistanceFromBody == 1.0f && lastDistanceFromBodyRight != 1.0f)
        //    {
        //        instrument.AddNote((byte)(point.X / 10), .1, 127);
        //    }
        //    lastDistanceFromBodyRight = point.DistanceFromBody;
        //}

        //private byte NormaliseHandPosition(int position)
        //{
        //    return 1;
        //}

        void ControllerImageSource(ColorImageFrame colorImage)
        {
            mainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, new Action<ColorImageFrame>(UpdateImage), colorImage);
        }

        private void UpdateImage(ColorImageFrame colorImage)
        {
            mainWindow.KinectImage.Source = colorImage.ToBitmapSource();
        }
    }
}
